package top.xana.acg.anime.ui.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.text.TextUtils
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import top.xana.acg.anime.R
import top.xana.acg.anime.utils.oval

class ImageView @JvmOverloads constructor(
    protected var mCtx: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatImageView(
    mCtx, attrs, defStyleAttr
) {
    private var width = 0f
    private var height = 0f
    private val defaultRadius = 16
    private var radius = 0
    private var leftTopRadius = 0
    private var rightTopRadius = 0
    private var rightBottomRadius = 0
    private var leftBottomRadius = 0
    private var o = false
    private var url: String? = null
    private fun init(context: Context, attrs: AttributeSet?) {

        // 读取配置
        val array = context.obtainStyledAttributes(attrs, R.styleable.ImageView)
        o = array.getBoolean(R.styleable.ImageView_oval, false)
        if (!o) {
            radius = array.getDimensionPixelOffset(R.styleable.ImageView_radius, defaultRadius)
            leftTopRadius =
                array.getDimensionPixelOffset(R.styleable.ImageView_left_top_radius, defaultRadius)
            rightTopRadius =
                array.getDimensionPixelOffset(R.styleable.ImageView_right_top_radius, defaultRadius)
            rightBottomRadius =
                array.getDimensionPixelOffset(
                    R.styleable.ImageView_right_bottom_radius,
                    defaultRadius
                )
            leftBottomRadius =
                array.getDimensionPixelOffset(
                    R.styleable.ImageView_left_bottom_radius,
                    defaultRadius
                )

            //如果四个角的值没有设置，那么就使用通用的radius的值。
            if (defaultRadius == leftTopRadius) {
                leftTopRadius = radius
            }
            if (defaultRadius == rightTopRadius) {
                rightTopRadius = radius
            }
            if (defaultRadius == rightBottomRadius) {
                rightBottomRadius = radius
            }
            if (defaultRadius == leftBottomRadius) {
                leftBottomRadius = radius
            }
        }

        url = array.getString(R.styleable.ImageView_url)

        array.recycle()
    }

    fun setRadius(r: Int) {
        radius = r
    }

    fun setUrl(u: String?) {
        url = u
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        width = getWidth().toFloat()
        height = getHeight().toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        if (o) oval()
        else {
            //这里做下判断，只有图片的宽高大于设置的圆角距离的时候才进行裁剪
            val maxLeft = Math.max(leftTopRadius, leftBottomRadius)
            val maxRight = Math.max(rightTopRadius, rightBottomRadius)
            val minWidth = maxLeft + maxRight
            val maxTop = Math.max(leftTopRadius, rightTopRadius)
            val maxBottom = Math.max(leftBottomRadius, rightBottomRadius)
            val minHeight = maxTop + maxBottom
            if (width >= minWidth && height > minHeight) {
                val path = Path()
                //四个角：右上，右下，左下，左上
                path.moveTo(leftTopRadius.toFloat(), 0f)
                path.lineTo(width - rightTopRadius, 0f)
                path.quadTo(width, 0f, width, rightTopRadius.toFloat())
                path.lineTo(width, height - rightBottomRadius)
                path.quadTo(width, height, width - rightBottomRadius, height)
                path.lineTo(leftBottomRadius.toFloat(), height)
                path.quadTo(0f, height, 0f, height - leftBottomRadius)
                path.lineTo(0f, leftTopRadius.toFloat())
                path.quadTo(0f, 0f, leftTopRadius.toFloat(), 0f)
                canvas.clipPath(path)
            }
        }
        if (!TextUtils.isEmpty(url)) {
            setSrc(url)
        }
        super.onDraw(canvas)
    }

    fun setSrc(src: String?) {
        Glide.with(mCtx).load(src)
            .crossFade()
            .centerCrop()
            .into(this)
    }

    init {
        init(mCtx, attrs)
    }
}